<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>监听DOM元素属性变化</title>
  <style>
    .box{
      width: 200px;
      height: 200px;
      border: 1px solid #000;
      background-color: yellow;
    }
  </style>
</head>
<body>
  <div class="box"></div>
  <script>
    const boxNode = document.querySelector('div.box');
    const observer = new MutationObserver((mutations)=>{
      console.log(mutations);
      // 可以断点调试，查看callStack找到引发变化的代码
    })
    if(boxNode){
      observer.observe(boxNode,{
        attributes: true,
        childList: true, // 子元素也监听
        attributeFilter: ['style'] //
      })
    }
    // test
    setTimeout(()=>{
      boxNode.style.backgroundColor = 'pink';
    }, 2000)
  </script>
</body>
</html>